package com.teacold.customdemo.chapter_seven;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.CollapsibleActionView;
import android.view.MotionEvent;
import android.view.View;

import com.teacold.customdemo.R;

public class MagnifierView extends View {
    private Paint mPaint;
    private Bitmap mBitmap,mBitmapBG;
    private int mDY,mDX;


    public MagnifierView(Context context) {
        super(context);
        mPaint=new Paint();
        mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.timg);
    }

    public MagnifierView(Context context, AttributeSet attrs) {
        this(context);
    }

    public MagnifierView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mBitmapBG==null){
            mBitmapBG=Bitmap.createBitmap(getWidth(),getHeight(),Bitmap.Config.ARGB_8888);
            Canvas canvas1=new Canvas(mBitmapBG);
            canvas1.drawBitmap(mBitmap,null,new Rect(0,0,getWidth(),getHeight()),mPaint);
        }
        if(mDX!=-1&&mDY!=-1){
            mPaint.setShader(new BitmapShader(mBitmapBG,Shader.TileMode.REPEAT,Shader.TileMode.REPEAT));
            canvas.drawCircle(mDX,mDY,150,mPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
       switch (event.getAction()){
           case MotionEvent.ACTION_DOWN:
               mDX= (int) event.getX();
               mDY= (int) event.getY();
               postInvalidate();
               return true;
           case MotionEvent.ACTION_MOVE:
              mDX= (int) event.getX();
              mDY= (int) event.getY();
               break;
           case MotionEvent.ACTION_UP:
           case MotionEvent.ACTION_CANCEL:
              mDY=-1;
              mDX=-1;
               break;
       }
          postInvalidate();
        return super.onTouchEvent(event);
    }
}
